package com.epam.giorgadze.task4.Entity;

import com.epam.giorgadze.task4.LibraryException.ResourсeException;

import java.util.Random;

public class Client extends Thread{


    private int clientID;
    private boolean reading = false;
    private Library<Book> library;

    public int getClientID() {
        return clientID;
    }

    public void setClientID(int clientID) {
        this.clientID = clientID;
    }

    public Client(Library<Book> library) {
        Random rand = new Random();
        this.library = library;
        this.clientID = rand.nextInt(15);
    }

    public void run() {
        Book book = null;
        try {
            book = library.getResource(500);
            reading = true;
            System.out.println("Library client#" + this.getClientID() + " took " + book.toString());
            book.using();
        } catch (ResourсeException ex) {
            System.out.println("Client#" + this.clientID + " has left"
                    + ex.getMessage());
        } finally {
            if (book != null) {
                reading = false;
                System.out.println("Library client#" + this.clientID +
                        " returned " + book.toString());
                library.returnResource(book);
            }
        }
    }
    
    public boolean isReading() {
        return reading;
    }
}
